import { Injectable, Logger } from '@nestjs/common';
import { ConfigService } from '@nestjs/config';

@Injectable()
export class AppService {
  private readonly logger = new Logger(AppService.name);

  constructor(private readonly configService: ConfigService) {}

  getAppInfo() {
    this.logger.log('获取应用信息');
    
    return {
      name: 'KV Admin',
      description: 'KV博客管理后台API',
      version: '1.0.0',
      environment: this.configService.get('NODE_ENV', 'development'),
      uptime: process.uptime(),
      timestamp: new Date().toISOString(),
    };
  }

  healthCheck() {
    this.logger.log('执行健康检查');
    
    const memoryUsage = process.memoryUsage();
    
    return {
      status: 'ok',
      timestamp: new Date().toISOString(),
      uptime: process.uptime(),
      memory: {
        rss: `${Math.round(memoryUsage.rss / 1024 / 1024)}MB`,
        heapTotal: `${Math.round(memoryUsage.heapTotal / 1024 / 1024)}MB`,
        heapUsed: `${Math.round(memoryUsage.heapUsed / 1024 / 1024)}MB`,
        external: `${Math.round(memoryUsage.external / 1024 / 1024)}MB`,
      },
      node: {
        version: process.version,
        platform: process.platform,
        arch: process.arch,
      },
    };
  }

  getVersion() {
    this.logger.log('获取版本信息');
    
    const packageJson = require('../../../package.json');
    
    return {
      app: 'kv-admin',
      version: packageJson.version || '1.0.0',
      nodeVersion: process.version,
      environment: this.configService.get('NODE_ENV', 'development'),
      buildTime: new Date().toISOString(),
    };
  }
}
